package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import main.BaseGeneration.comEntry;

public class KeywordsGeneration extends BaseGeneration {

	public final static String STOP_WORDS_FILENAME = "data/stopword.txt";

	public final static String KEY_SENTENCE_FILENAME = "data/adhoc05";

	public final static String OUTPUT_FULLTERMS_FILENAME = "data/topic-fullterms";

	private List<String> stopWords = new ArrayList<String>();

	@Override
	void gen() {
		try {
			readStopWords(STOP_WORDS_FILENAME);
			readKeySentence(KEY_SENTENCE_FILENAME);
			BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT_FULLTERMS_FILENAME));
			Set<Entry<String, Topic>> set = topics.entrySet();
			List<Entry<String, Topic>> list = new ArrayList<Entry<String, Topic>>(set);
			Collections.sort(list, new comEntry());
			for (Iterator<Entry<String, Topic>> iter = list.iterator(); iter.hasNext();) {
				Topic topic = iter.next().getValue();
				genData(topic);
				write(topic, bw);
			}
			bw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void write(Topic topic, BufferedWriter bw) throws IOException {
		StringBuffer sb = new StringBuffer();
		sb.append(topic.getId()).append(",");
		sb.append(StringUtils.join(topic.getFullTerms(), " "));
		bw.write(sb.toString() + "\r\n");
	}

	/**
	 * @param topic
	 */
	private void genData(Topic topic) {
		String[] terms = WordSegmentationUtil.splitWords(topic.getSentence());
		List<String> fullTerms = new ArrayList<String>();
		for (int i = 0; i < terms.length; i++) {
			// remove stop words,duplicate words,single letter
			if (!fullTerms.contains(terms[i]) && !stopWords.contains(terms[i]) && terms[i].length() > 1) {
				fullTerms.add(terms[i]);
			}
		}
		Collections.sort(fullTerms);
		topic.setFullTerms(fullTerms);
	}

	/**
	 * @param filename
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private void readStopWords(String filename) throws FileNotFoundException, IOException {
		BufferedReader top_file = new BufferedReader(new FileReader(filename));
		Pattern p = Pattern.compile("H(.*),:");
		for (String tmp = top_file.readLine(); tmp != null; tmp = top_file.readLine()) {
			Matcher m = p.matcher(tmp);
			if (m.find()) {
				stopWords.add(m.group(1));
			}
		}
		top_file.close();
	}

	private void readKeySentence(String filename) throws FileNotFoundException, IOException {
		BufferedReader top_file = new BufferedReader(new FileReader(filename));
		Pattern p = Pattern.compile("<(\\d+)>(.*)");
		for (String tmp = top_file.readLine(); tmp != null; tmp = top_file.readLine()) {
			Matcher m = p.matcher(tmp);
			if (m.find()) {
				Topic topic = new Topic();
				topic.setId(m.group(1));
				topic.setSentence(m.group(2).toLowerCase());
				topics.put(topic.getId(), topic);
			}
		}
		top_file.close();
	}

	public static void main(String[] args) {
		KeywordsGeneration kg = new KeywordsGeneration();
		kg.gen();
	}

}
